#!/usr/bin/env node

// 实时PWM命令监控工具
const WebSocket = require('ws');

console.log("启动PWM实时监控...");
console.log("连接到 Network RC WebSocket...");

const ws = new WebSocket('ws://192.168.1.19:8080/control');

ws.on('open', function open() {
  console.log('✅ WebSocket连接成功');
  
  // 登录
  ws.send(JSON.stringify({
    action: 'login',
    payload: {}
  }));
  
  console.log('📡 开始监控PWM命令...');
  console.log('请在前端操作滑动油门，观察命令输出');
  console.log('---');
});

ws.on('message', function message(data) {
  const msg = JSON.parse(data.toString());
  console.log(`[接收] ${msg.action}:`, msg.payload);
});

ws.on('error', function error(err) {
  console.error('❌ WebSocket错误:', err.message);
});

ws.on('close', function close() {
  console.log('❌ WebSocket连接关闭');
});

// 定期发送心跳
setInterval(() => {
  if (ws.readyState === WebSocket.OPEN) {
    ws.send(JSON.stringify({
      action: 'heartbeat',
      payload: {}
    }));
  }
}, 1000);

// 10分钟后自动退出
setTimeout(() => {
  console.log('监控结束');
  ws.close();
  process.exit(0);
}, 600000);